# -*- coding: UTF-8 -*- #存在中文字体，所有要制定字符集为UTF-8


'''
start line,just for unix
module doc 
module import 
variable define
class define
function define
if __name__=='__main__': #main startup
    function invoke
'''

'''
python通过_name_来达到检测模块的设计意图
模块是对象，并且所有的模块都有一个内置属性 __name__。
一个模块的 __name__ 的值取决于您如何应用模块。如果 import 一个模块，那么模块__name__ 的值通常为模块文件名，不带路径或者文件扩展名。
可以像一个标准的程序样直接运行模块，在这 种情况下, __name__ 的值将是一个特别缺省"__main__"。

直接运行.py文件,则__name__的值是'__main__';
在import 一个.py文件后,__name__的值就不是'__main__'了,通常是模块文件名;
通过if __name__ == '__main__'来判断是否是在直接运行该.py文件
'''
if __name__ == "__main__":
    print "this program is running by itself";
else :
    print __name__ + " program is imported by another module ";

def function_name():
    print("module function exec");
    
    

def jdbc():
    """
    connection is a physical concept, present communication channel,a socket connection.
    session is a logical concept, define a communication action,but connection decide when the action occur.
    one connection allowed own several session or no on, each session not affect each other
    """
    import MySQLdb;
    
    conn = MySQLdb.connect(host='localhost', user='root', passwd='root', db='issh');
    
    conn.begin();
    cursor = conn.cursor();
    descr = "python";
    cursor.execute("insert into tb_jpa (descr) values(%s)", descr);
    conn.commit();  # connection not setting auto commit,so should do it directly
    
    
    cursor.execute("select * from tb_jpa where descr=%s", descr);
    print "Rows selected : ", cursor.rowcount;
    for row in cursor.fetchall():
        print "note : ", row[0];
    cursor.close();
    
    conn.close();
    
def oracle():
    import cx_Oracle
    conn = cx_Oracle.connect('tyqk/karakal@192.168.1.213:1521/orcl')    
    try:
        cursor = conn.cursor () 
        cursor.execute ("SELECT t.* FROM tb_song t where t.song_id = '1000000'")  
        rows = cursor.fetchall()  
        for row in rows:  
            print row
    finally:    
        conn.close()
     
        
def oraclepool():
    import cx_Oracle
    session_factory = cx_Oracle.SessionPool(user='tyqk', password='karakal', dsn='192.168.1.213:1521/orcl', min=5, max=10, increment=1, connectiontype=cx_Oracle.Connection, threaded=True, getmode=cx_Oracle.SPOOL_ATTRVAL_NOWAIT, homogeneous=True);
    
    session = session_factory.acquire()
    try:
        cursor = session.cursor()
        cursor.execute(" select t.* from tb_song t where t.song_id = '1000000' ")
        rows = cursor.fetchall()
        for row in rows:
            print row
    finally:
        session_factory.release(session)
        
